package com.linsen.core.common.util;

import cn.hutool.core.util.ObjectUtil;
import com.linsen.core.common.enums.BrowserTypeEnum;
import lombok.experimental.UtilityClass;
import org.springframework.http.HttpHeaders;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Description: 浏览器工具类
 * @ProjectName: ccadult-work-weixin-platform
 * @Package: com.lanshan.commons.tools.utils
 * @ClassName: BrowserUtils
 * @Author: zhaoyong
 * @Email: zy765203718@gmail.com
 * @Date: 2022/1/24 15:05
 * @Version: 1.0
 */
@UtilityClass
public final class BrowserUtils {
    // 判断是否是IE
    public boolean isIE(HttpServletRequest request) {
        String userAgent = request.getHeader(HttpHeaders.USER_AGENT).toLowerCase();
        return userAgent.indexOf("msie") > 0 || userAgent.indexOf("rv:11.0") > 0;
    }

    /**
     * 获取IE版本
     *
     * @param request
     * @return
     */
    public Double getIEversion(HttpServletRequest request) {
        Double version;
        BrowserTypeEnum browserTypeEnum = getBrowserType(request);
        switch (browserTypeEnum){
            case IE11:
                version = 11.0;
                break;
            case IE10:
                version = 10.0;
                break;
            case IE9:
                version = 9.0;
                break;
            case IE8:
                version = 8.0;
                break;
            case IE7:
                version = 7.0;
                break;
            case IE6:
                version = 6.0;
                break;
            default:
                version = 0.0;
        }

        return version;
    }

    /**
     *
     * @Title: encodeFileName
     * @Description: 根据不同浏览器类型编码中文文件名
     * @param request
     * @param filename
     * @return
     * @throws UnsupportedEncodingException String
     * @author zhaoyong
     * @date 2019年2月11日上午10:13:14
     */
    public String encodeFileName(HttpServletRequest request,String filename) throws UnsupportedEncodingException {
        BrowserTypeEnum browserTypeEnum = getBrowserType(request);
        switch(browserTypeEnum){
            case IE11:
            case IE10:
            case IE9:
            case IE8:
            case IE7:
            case IE6:
                filename = URLEncoder.encode(filename, "UTF-8");
                break;
            case FIREFOX:
            case CHROME:
                filename = new String(filename.getBytes(),"ISO8859-1");
                break;
            default:
                filename = new String(filename.getBytes(),"UTF-8");
        }
        return filename;
    }

    /** 获取Request请求对应的浏览器类型 */
    public BrowserTypeEnum getBrowserType(HttpServletRequest request) {
        return getBrowserType(request.getHeader(HttpHeaders.USER_AGENT));
    }

    public BrowserTypeEnum getBrowserType(String userAgent) {
        if (ObjectUtil.isNotEmpty(userAgent)){
            for (BrowserTypeEnum browserTypeEnum : BrowserTypeEnum.values()) {
                if (regex(browserTypeEnum.value(),userAgent)){
                    return browserTypeEnum;
                }
            }
        }

        return BrowserTypeEnum.UNKNOWN;
    }

    public boolean regex(String regex, String str) {
        Pattern p = Pattern.compile(regex, Pattern.MULTILINE);
        Matcher m = p.matcher(str);
        return m.find();
    }
}

